import axios from "axios";
import storage from '../utils/storage'
import {Toast} from 'antd-mobile'
const service = axios.create ({
        baseURL:'/api',
        timeout: 10000
});
service.interceptors.request.use (config => {
        console.log(config)
        config.method = 'POST'
        config.headers['PLATFORM'] = 'Android'
        config.headers['APP_VERSION'] = '1.0.0'
        config.headers['LYB_ACCESS_TOKEN_'] = storage.get('token')||''
        if (config.url === '/wap/member/image/upload') {
                config.headers['Content-Type'] = 'multipart/form-data'
        }

        return config;
}, error => {
        Promise.reject (error)
})
service.interceptors.response.use (response => {
                const {status} = response
                if (status === 200) {
                        const {code,errorMsg,data,token} = response.data
                        if (code=== 0) {
                                if (token) {
                                        storage.set('token',token)
                                }
                                return Promise.resolve(data)
                        }else{
                                Toast.offline(errorMsg, 1);
                                return Promise.reject(response.data)
                        }
                }else{
                        Toast.offline('网络超时', 1);
                }
        },
        error => {
                console.dir(error.status)
                Toast.offline('网络超时', 1);
        }
)
export default service
